package com.components.form
{
	/**
	 * @auth Chunning
	 * @purpose check method....
	 * @date 2009-4-2
	 * */
	import com.components.form.element.IdentifyingCode;
	
	import flash.text.TextField;
	
	import string.ExtendString;
	
	public class InternalCheckManager
	{
		
		private var _checkList:Array;
		
		//get check list...list of method...
		public static function get checkList():Array
		{
			
			return [{name:"blank",check:blankCheck}];
			
		}
		
		//get method by name...
		public static function getMthodByName(name:String):Function
		{
			
			var check:Function = InternalCheckManager[name + "Check"];
						
			return check;
			
		}
		
				
		//check input length...
		private static function blankCheck(item:FormItem):void
		{
			
			var blank:TextField = item.item;
			
			var form:FormController = item.form;
			
			if(ExtendString.trim(blank.text).length == 0)
			{
												
				item.returnCheckResult("blank",false);
								
				item.form.dispatchEvent(new FormEvents(FormEvents.IS_BLANK,item));	
												
			}else{
				
				form.getInfoByName(item.name).text = "";
																			
				item.returnCheckResult("blank",true);
				
			}
						
		}
		
		//check format is correct...
		private static function emailCheck(item:FormItem):void
		{
			
			var pattern:RegExp = /[\w\.\-]+@[a-zA-Z0-9\-]+(\.\w+)+/gi;
			
			pattern.lastIndex = 0;
									
			if(pattern.test(item.item.text) && pattern.lastIndex == item.item.text.length && item.item.text.search(pattern) == 0)
			{
				
				item.form.getInfoByName(item.name).text = "";
													
				item.returnCheckResult(item.name,true);
								
			}else{
				
				item.returnCheckResult(item.name,false);
															
				item.form.dispatchEvent(new FormEvents(FormEvents.EMAIL_UNAVAILABEL,item));
											
			}
			
		}
		
		//check password...
		private static function passwordCheck(item:FormItem):void
		{
			
			if(item.unitCheckItem == null)
			{
				
				item.returnCheckResult(item.name,true);
				
				return;
				
			}
			
			repasswordCheck(item);
			
		}
		
		//check repeat...
		private static function repasswordCheck(item:FormItem):void
		{
			
			var matchName:String = item.name == "password" ? "repassword" : "password";
			
			var txtPwd:TextField = item.form.getItemByName(matchName).item;
						
			if(item.item.text == txtPwd.text)
			{
				
				item.form.getInfoByName(item.name).text = "";
				
				item.returnCheckResult(item.name,true);
								
			}else{
								
				item.returnCheckResult(item.name,false);
																
				item.form.dispatchEvent(new FormEvents(FormEvents.REPEAT_NOT_MATCH,item));
			}
						
		}
		
		//check idefint code...
		private static function identifyCheck(item:FormItem):void
		{
			
			var identify:IdentifyingCode = item.form.getConsoleByName("code") as IdentifyingCode;
			
			if(identify.check(item.item.text))
			{
				
				item.form.getInfoByName(item.name).text = "";
													
				item.returnCheckResult(item.name,true);
								
			}else{
				
				item.returnCheckResult(item.name,false);
								
				identify.refresh();
								
				item.form.dispatchEvent(new FormEvents(FormEvents.IDENTIFY_CODE_WRONG,item));
							
			}
			
		}
		
		//END
	}
}